package com.cloud.shopping.tool;

import java.io.Serializable;

/**
 * @author John.zhang
 * @version Id: PageInfo, v 1.0 2021/1/3 11:58 ZhiYong Exp $
 */
public class PageInfo implements Serializable {

    /**
     * 页数（第几页）
     */
    private long currentPage;

    /**
     * 数据记录数
     */
    private long total;

    /**
     * 页数
     */
    private int size;

    /**
     * 下页
     */
    private int next;

    /**
     * 最后一页
     */
    private int last;

    private int left;

    private int right;

    /**
     * 从哪条开始查
     */
    private long start;

    /**
     * 全局偏移量
     */
    public int offSize = 2;

    public PageInfo() {
        super();
    }

    public void setCurrentPage(long currentPage, long total, long pageSize) {
        //可以整除的情况下
        long pageCount = total / pageSize;

        //如果整除表示正好分N页，如果不能整除在N页的基础上+1页
        int totalPages = (int) (total % pageSize == 0 ? total / pageSize : (total / pageSize) + 1);

        //总页数
        this.last = totalPages;

        //判断当前页是否越界,如果越界，我们就查最后一页
        if (currentPage > totalPages) {
            this.currentPage = totalPages;
        } else {
            this.currentPage = currentPage;
        }

        //计算start
        this.start = (this.currentPage - 1) * pageSize;
    }

    //上一页
    public long getUpper() {
        return currentPage > 1 ? currentPage - 1 : currentPage;
    }

    //总共有多少页，即末页
    public void setLast(int last) {
        this.last = (int) (total % size == 0 ? total / size : (total / size) + 1);
    }

    /**
     * 带有偏移量设置的分页
     *
     * @param total       total
     * @param currentPage currentPage
     * @param pageSize    pageSize
     * @param offSize     offSize
     */
    public PageInfo(long total, int currentPage, int pageSize, int offSize) {
        this.offSize = offSize;
        initPage(total, currentPage, pageSize);
    }


    public PageInfo(long total, int currentPage, int pageSize) {
        initPage(total, currentPage, pageSize);
    }

    public void initPage(long total, int currentPage, int pageSize) {
        //总记录数
        this.total = total;
        //每页显示多少条
        this.size = pageSize;

        //计算当前页和数据库查询起始值以及总页数
        setCurrentPage(currentPage, total, pageSize);

        //分页计算
        int leftCount = this.offSize,
                //需要向上一页执行多少次
                rightCount = this.offSize;

        //起点页
        this.left = currentPage;
        //结束页
        this.right = currentPage;

        //2点判断
        this.left = currentPage - leftCount;            //正常情况下的起点
        this.right = currentPage + rightCount;        //正常情况下的终点

        //页差=总页数和结束页的差
        int top = this.last - right;                //判断是否大于最大页数

        //起点页
        // 1、页差<0  起点页=起点页+页差值
        // 2、页差>=0 起点和终点判断
        this.left = top < 0 ? this.left + top : this.left;

        //结束页
        // 1、起点页<=0   结束页=|起点页|+1
        // 2、起点页>0    结束页
        this.right = this.left <= 0 ? this.right + (this.left * -1) + 1 : this.right;

        // 当起点页<=0  让起点页为第一页
        //
        // 否则不管
        this.left = this.left <= 0 ? 1 : this.left;

        // 如果结束页>总页数   结束页=总页数
        // 否则不管
        this.right = Math.min(this.right, last);
    }

    public long getCurrentPage() {
        return currentPage;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public int getNext() {
        return next;
    }

    public void setNext(int next) {
        this.next = next;
    }

    public int getLast() {
        return last;
    }

    public int getLeft() {
        return left;
    }

    public void setLeft(int left) {
        this.left = left;
    }

    public int getRight() {
        return right;
    }

    public void setRight(int right) {
        this.right = right;
    }

    public long getStart() {
        return start;
    }

    public void setStart(long start) {
        this.start = start;
    }

    public int getOffSize() {
        return offSize;
    }

    public void setOffSize(int offSize) {
        this.offSize = offSize;
    }
}
